#!/usr/bin/env node

const url  = require('url'),
      fs   = require('fs'),
      http = require('http'),
      movieList = require('./data.js');

//console.log(movieList);
var arr = [];
var j = 0;
var m = 0;

for(let i = 0; i < movieList.length; i++){
  if(movieList[i].movieRange != '暂无评分'){
    arr[j] = movieList[i];
    if(j > 0){
      for(m = j; m > 0; m--){
        if(arr[m].movieRange > arr[m-1].movieRange){
          let temp = arr[m];
          arr[m] = arr[m-1];
          arr[m-1] = temp;
        }
      }
    }
    j++;
  }
}
//console.log(arr);

let html = fs.readFileSync('./index.html').toString('utf8');

http.createServer((req,res) => {
  let ads = url.parse(req.url);
  if(ads.pathname === '/getlist'){
    res.writeHead(200,{
      'Content-type':'application/json'
    }); 
    
    let a = JSON.stringify(arr);
    res.end(a);
  }
  res.end(html);
}).listen(8080);
